{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "58ab0980-cbb4-41f0-b5d2-c990cd5f9aef",
   "metadata": {},
   "source": [
    "有时，我们要为整个应用添加依赖项。\r\n",
    "\r\n",
    "通过与定义路径装饰器依赖项 类似的方式，可以把依赖项添加至整个 FastAPI 应用。\r\n",
    "\r\n",
    "这样一来，就可以为所有路径操作应用该依赖项："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3150c552-98d8-42c8-99d9-da0dc2f9f011",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [24848]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://0.0.0.0:8009 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:49336 - \"GET /items/ HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:49366 - \"GET /users/ HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [24848]\n"
     ]
    }
   ],
   "source": [
    "import uvicorn\n",
    "from fastapi import Depends, FastAPI, Header, HTTPException\n",
    "\n",
    "async def verify_token(x_token: str = Header()):\n",
    "    if x_token != \"fake-super-secret-token\":\n",
    "        raise HTTPException(status_code=400, detail=\"X-Token header invalid\")\n",
    "\n",
    "\n",
    "async def verify_key(x_key: str = Header()):\n",
    "    if x_key != \"fake-super-secret-key\":\n",
    "        raise HTTPException(status_code=400, detail=\"X-Key header invalid\")\n",
    "    return x_key\n",
    "\n",
    "app = FastAPI(dependencies=[Depends(verify_token), Depends(verify_key)])\n",
    "\n",
    "@app.get(\"/items/\")\n",
    "async def read_items():\n",
    "    return [{\"item\": \"Portal Gun\"}, {\"item\": \"Plumbus\"}]\n",
    "\n",
    "\n",
    "@app.get(\"/users/\")\n",
    "async def read_users():\n",
    "    return [{\"username\": \"Rick\"}, {\"username\": \"Morty\"}]\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    config = uvicorn.Config(app, host='0.0.0.0', port=8009)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2f63466-f5d9-4a00-8265-9c1c140bfd41",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'http://127.0.0.1:8009/items/' \n",
    "headers = {\"x-token\": \"fake-super-secret-token\",\"x-key\": \"fake-super-secret-key\"} \n",
    "res = requests.get(url, headers=headers) \n",
    "res.text\n",
    "\n",
    "# 在另一个ipynb文件中运行代码，会得到 '[{\"item\":\"Portal Gun\"},{\"item\":\"Plumbus\"}]'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3107fa62-b137-41d9-bd2a-cca58883607f",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'http://127.0.0.1:8009/users/' \n",
    "headers = {\"x-token\": \"fake-super-secret-token\",\"x-key\": \"fake-super-secret-key\"} \n",
    "res = requests.get(url, headers=headers) \n",
    "res.text\n",
    "\n",
    "# 在另一个ipynb文件中运行代码，会得到 '[{\"username\":\"Rick\"},{\"username\":\"Morty\"}]'"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
